<template>
  <div>
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <!-- 搜索 和 添加按钮 区域 -->
      <el-row :gutter="5">
        <!-- 输入框 -->
        <el-col :span="8">
          <el-input
            clearable
            placeholder="搜索商品"
            v-model="queryParams.query"
            @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <!-- 添加商品按钮 -->
        <el-col :span="6">
          <el-button type="primary" @click="toAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品表格区域 -->
      <el-table :data="goodsList" border style="width: 100%">
        <el-table-column type="index" label="#" width="30"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="110"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="200">
          <template slot-scope="scope">
            {{ scope.row.add_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-row>
              <el-tooltip content="编辑" placement="top">
                <el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-edit"
                  @click="showEditDialog(scope.row)"></el-button>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="deleteGoods(scope.row.goods_id)"></el-button>
              </el-tooltip>
            </el-row>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页栏 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pagenum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryParams.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 控制显示添加商品对话框
      addDialogVisible: false,
      // 商品列表数据
      goodsList: [],
      //
      // 请求查询参数
      queryParams: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 商品总数
      total: 0
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 搜索商品
    searchGoods() {},
    // 请求商品列表数据
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', { params: this.queryParams })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.goodsList = res.data.goods
      // console.log(res.data.goods)
      this.total = res.data.total
    },
    // 显示编辑商品对话框
    showEditDialog(row) {
      // console.log(row)
    },
    // 根据ID删除商品
    deleteGoods(gid) {
      // console.log(gid)
      // 警告弹框
      this.$confirm('是否确定删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(`goods/${gid}`)
          // console.log(res)
          if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
          this.$message.success(res.meta.msg)
          // this.getRolesList()
          this.getGoodsList()
        })
        .catch(() => this.$message.info('已取消删除'))
    },
    // 每页显示数量改变时触发
    handleSizeChange(newSize) {
      this.queryParams.pagesize = newSize
      this.getGoodsList()
    },
    // 当前页数改变触发
    handleCurrentChange(newPage) {
      this.queryParams.pagenum = newPage
      this.getGoodsList()
    },
    // 前往添加商品页面
    toAddPage() {
      this.$router.push('/goods/add')
    }
  }
}
</script>
<style lang="less" scoped></style>
